import java.util.Scanner;
import java.io.InputStream;
import java.io.FileInputStream;
class maze{

	public char[][] maze;
	private int size;
	private int x,y,sx,sy;
	public inn s = new inn();
	public int top=0;

	public maze(char[][] str,int len){
		for(int i=0;i<len;i++){
			for(int j=0;j<len;j++) maze[i][j] = str[i][j];
		}
	}
	public maze()throws Exception{
		InputStream in = new FileInputStream("img/a.txt");
		Scanner sc = new Scanner(in);
		String str = null;
		while(sc.hasNextLine()){
			str = sc.nextLine();
			break;
		}
		size = str.length();
		maze = new char[size][size];
		int i = 0;
		InputStream ini = new FileInputStream("img/a.txt");
		Scanner sct = new Scanner(ini);
		while(sct.hasNextLine()){
			str = sct.nextLine();
			maze[i++] = str.toCharArray();
		}
	}
	public void printMaze(){
		for(int i = 0;i<size;i++){
			for(int j = 0;j<size;j++){
				System.out.print(" "+maze[i][j]);
			}
			System.out.print("\n");
		}
	}
	public void runMaze(){
		int sign;
		this.getEnterse();
		s.push(x,y);
		this.signRoad(x,y);
		sx = x;
		sy = y;
		while((sign=this.getAcess(x,y))!=0){
			switch(sign){
				case 1: 
					s.push(x,++y);
					this.signRoad(x,y);
					break;
				case 2:
					s.push(++x,y);
					this.signRoad(x,y);
					break;
				case 3:
					s.push(--x,y);
					this.signRoad(x,y);
					break;
				case 4:
					s.push(x,--y);
					this.signRoad(x,y);
					break;
				case 5:
					maze[x][y] = '%';
					s.pop();
					x = s.x;
					y = s.y;
					maze[x][y] = '@';
			}
			System.out.println(x+","+y);
		}
		this.top = s.top;
		 
	}
	public void printMaze_rode(){

	}
	private void getEnterse(){
		sx = x = 1;
		sy = y = 0;
	}
	private void signRoad(int x,int y){
		this.maze[x][y] = '@';
	}
	//return :0 is ok;1 is right;2 is down;3 is left;4 is up;5 is none;
	private int getAcess(int x,int y){

		if(x==sx && y==sy){
			if(x==0 || x==size-1) if(maze[x][y+1]=='.' || maze[x][y-1]=='.') return 0;
			if(y==0 || y==size-1) if(maze[x+1][y]=='.' || maze[x-1][y]=='.') return 0;
		}
	 	if(x!=sx || y!=sy) if(x==0 || x==size-1 || y==0 || y==size-1) return 0;
		if(y+1<size && maze[x][y+1]=='.') return 1;
		else if(x+1<size && maze[x+1][y]=='.') return 2;
		else if(x-1>=0 && maze[x-1][y]=='.') return 3;
		else if(y-1>=0 && maze[x][y-1]=='.') return 4;
		else return 5;
	}

}